// Caesar Encrypt / Decrypt Program.
// By DreamVB 02:46 19/10/2016

#include <iostream>
#include <string>

using namespace std;
using std::cout;
using std::endl;

void CaesarCrypt(string src, int Secret, bool Encrypt){
	string Test = src;

	int i = 0;
	int ch = 0;
	int shift = Secret;

	if (!Encrypt){
		//Get correct shift code.
		shift = (26 - shift) % 26;
	}

	for (i = 0; i < Test.length(); i++){
		ch = Test[i];
		//Encrypt uppercase letters.
		if (isupper(ch)){
			Test[i] = (((ch - 'A' + shift)) % 26 + 'A');
		}
		//Encrypt lowercase letters.
		if (islower(ch)){
			Test[i] = (((ch - 'a' + shift)) % 26 + 'a');
		}
	}
	//Output string
	cout << Test.c_str();
}

void ShowMenu(bool IsEncrypting){
	char temp[260];
	string message = "";
	char *pstr;
	int key = 1;
	
	//Clear screen
	system("cls");

	if (IsEncrypting){
		cout << "Emter a message to encrypt : ";
	}
	else{
		cout << "Enter encrypted test : ";
	}

	//Get string from user.
	cin.ignore();
	std::getline(std::cin, message);

	cout << "Enter secret key between 1 and 26 : ";
	cin >> key;
	cout << endl;

	//Do the encryption / decryption.
	if (IsEncrypting){
		cout << "Message Decrypted:" << endl;
	}
	else{
		cout << "Encrypted Message:" << endl;
	}

	//Enceypt/Decrypt.
	CaesarCrypt(message, key, IsEncrypting);
	cout << endl << endl;
	
	system("pause");
}

int main(int argc, char *argv[]){
	int ch = 0;
	cout << "Caesar Encrypter/Decrypter" << endl;
	cout << "--------------------------" << endl;
	cout << " [1] Encrypt Message" << endl;
	cout << " [2] Decrypt Message" << endl;
	cout << " [3] Exit" << endl;
	cout << "Enter choice : ";
	cin >> ch;

	if (ch == 1){
		//Encrypt
		ShowMenu(true);
	}
	else if (ch == 2){
		//Decrypt
		ShowMenu(false);
	}
	else if (ch == 3){
		//Exit
		cout << endl << "Thanks for using this program." << endl;
		return 0;
	}
	else{
		cout << "Error with input choice." << endl;
		exit(1);
	}

	return 0;
}